﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyVoteMVC.Data;
using MyVoteMVC.Domain;
using MyVoteMVC.Common;
using MyVoteMVC.Common.Objects;
using MyVoteMVC.Data.Repositories;

namespace MyVoteMVC.Service
{
    public class VoteActivityImageService : IActivityImageService
    {
        private const string EmailAddress = "@gzdi.com";//接收方邮箱    
        IUnitOfWork unitOfWork;
        IRepository<VT_ActivityImage> activityImageRepository;

        public VoteActivityImageService()
        {
            unitOfWork = Factory.GetInstance<IUnitOfWork>();
            activityImageRepository = unitOfWork.GetRepository<VT_ActivityImage>();    
        }

        //增加
        public bool AddActivityImage(string activityID, byte[] activityImage)
        {
            VT_ActivityImage v_ActivityImage = new VT_ActivityImage();
            unitOfWork.BeginTransaction();
            v_ActivityImage.Activity_ID = activityID;
            v_ActivityImage.Activity_Image = activityImage;
            activityImageRepository.Add(v_ActivityImage);
            unitOfWork.CommitTransaction();
            return true;
        }
        //删除
        public bool DeleteActivityImage(string activityID)
        {
            VT_ActivityImage v_ActivityImage = activityImageRepository.GetEntityById(activityID);
            if (v_ActivityImage != null)
            {
                unitOfWork.BeginTransaction();
                activityImageRepository.Delete(v_ActivityImage);
                unitOfWork.CommitTransaction();
                return true;
            }
            return false;
        }
        //修改
        public bool ModifyActivityImage(string activityID, byte[] activityImage)
        {
            VT_ActivityImage v_ActivityImage = activityImageRepository.GetEntityById(activityID);
            if (v_ActivityImage != null)
            {
                unitOfWork.BeginTransaction();
                v_ActivityImage.Activity_Image = activityImage;
                activityImageRepository.Update(v_ActivityImage);
                unitOfWork.CommitTransaction();
                return true;
            }
            return false;
        }

        #region IDisposable Members
        private bool m_disposed;
        protected virtual void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                if (disposing)
                {
                    unitOfWork.Dispose();
                }

                // Release unmanaged resources

                m_disposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~VoteActivityImageService()
        {
            Dispose(false);
        }
        #endregion
    }
}
